#include "treenode.h"
int line  = 0;
void printBlank(int num)
{
    for(int i = 0;i < num;i++)
        printf(" ");
}

void printTree(int blank, treenode* root)
{
    if(!root)
        return;
    if(!root->terminal && root->capacity != 0)
        {//printf("%d ",++line);
            printBlank(blank);
            printf("%s (%d)\n",root->typename, root->line);
        }
    else if(root->terminal)
    {   //printf("%d ",++line);
        printBlank(blank);
        printf("%s", root->typename);
        if(strcmp("ID", root->typename) == 0 || strcmp("TYPE", root->typename) == 0 || strcmp("INT", root->typename) == 0 ||strcmp("FLOAT", root->typename) == 0)
            printf(": %s",root->val);
        printf("\n");
    }

    /*if(strcmp(root->typename, "Stmt") == 0 || strcmp(root->typename,"Def")==0)
    {
        for(int i = 0;i <root->capacity;i++)
        {
            printf("%d %s %s",root->next[i]->terminal, root->next[i]->typename, root->next[i]->val);
        }
    }*/

    if(root->capacity !=0 )
        for(int i = 0;i < root->capacity; i++)
            {printTree(blank+2, root->next[i]);}
}

treenode* newTNode(char* lexTypeName, char* val)
{
    treenode* node = (treenode* )malloc(sizeof(treenode));
    node->next = NULL;
    node->terminal = 1;
    strcpy(node->typename, lexTypeName);
    if(strcmp(lexTypeName,"TYPE") == 0|| strcmp(lexTypeName,"INT") == 0 || strcmp(lexTypeName,"FLOAT") == 0|| strcmp(lexTypeName,"ID") == 0)
        strcpy(node->val, val); 
    return node;
}

/*treenode* Insert(int num, YYLTYPE loc, ...)
{
    treenode* parent = newNode();
    parent->capacity = num;
    parent->line = loc.first_line;
    if(num == 0)
        return parent;
    parent->next = (treenode** )malloc(sizeof(treenode*) * num);
    va_list vap;
    va_start(vap,num);
    for(int i = 0;i < num; i++)
    {
        parent->next[i] = va_arg(vap, treenode*);
    }
    va_end(vap);
    return parent;
}*/